package com.lh.runmanage.controller;

import com.lh.runmanage.model.InPutParam.ProjectAndProductInParam;
import com.lh.runmanage.model.ProjectD4Model;
import com.lh.runmanage.model.ResultVO;
import com.lh.runmanage.service.ProjectAndProductService;
import com.lh.runmanage.service.ProjectService;
import com.lh.runmanage.unit.GetPropertiesClass;
import com.lh.runmanage.unit.LhClass;
import com.lh.runmanage.unit.ResultStruct;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：梁昊，后端工程师：梁昊
 * @create 2021-07-21 15:37
 * @function
 * @editLog
 */
@RestController
@RequestMapping("/projectAndProductController")
@Api(value = "仅展示产品与项目的关系", description = "系统运行态势平台展示湍")
public class ProjectAndProductController {
    @Autowired
    ProjectAndProductService projectAndProductService;
    @Autowired
    GetPropertiesClass getPropertiesClass;
    @Autowired
    ProjectService projectService;

    /**
     * 展示端方法：String， 产品维度显示项目名称，方法ID：SE202107211511245489B0522361069
     *
     * @param productId 表vou_productAndProject,字段名id:主键
     * @param projectId 表vou_productAndProject,原字段名id,现字段名projectId:主键
     * @return 树
     */
    @ApiOperation(value = "展示端方法：D6， 产品维度显示项目名称[树],方法ID：SE202107211511245489B0522361069", notes = "/projectAndProductController/V1/selectProjectAndProductD6?id=id&paraId=paraId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "产品CODE列表", dataType = "String", paramType = "query")
            , @ApiImplicitParam(name = "projectId", value = "项目CODE列表", dataType = "String", paramType = "query")
    })
    @PostMapping("/V1/selectProjectAndProductD6")
    public ResultVO selectProjectAndProductD6(@RequestParam(value = "productId", required = false) List<String> productId
            , @RequestParam(value = "projectId", required = false) List<String> projectId) {
        final List<String> idOrderBy = get20IdOrderBy();
        if (idOrderBy != null) {
            ProjectAndProductInParam projectAndProductInParam = new ProjectAndProductInParam();
            projectAndProductInParam.setId(idOrderBy);
            return selectProjectAndProduct(productId, projectId);
        } else
            return ResultStruct.success(null);
    }

    private ResultVO selectProjectAndProduct(List<String> id
            , List<String> projectId) {
        ProjectAndProductInParam projectAndProductInParam = new ProjectAndProductInParam();
        projectAndProductInParam.setId(id);
        projectAndProductInParam.setProjectId(projectId);
        return ResultStruct.success(projectAndProductService.selectProjectAndProductD6(projectAndProductInParam));
    }

    private List<ProjectD4Model> getProject20OrderBy() {
        return projectService.selectProjectD4OrderBy();
    }

    private List<String> get20IdOrderBy() {
        final List<ProjectD4Model> projectD4Models = getProject20OrderBy();
        if (projectD4Models != null) {
            List<String> list = new ArrayList<>(projectD4Models.size());
            for (ProjectD4Model item :
                    projectD4Models) {
                list.add(item.getId());
            }
            projectD4Models.clear();
            return list;
        } else {
            return null;
        }
    }

    /**
     * 产品ID，得到项目ID列表，方法ID：SE20210724145741533389732A93233
     *
     * @param paraId 表vou_productAndProject,字段名paraId:父ID
     * @return 项目ID列表
     */
    @ApiOperation(value = " 产品ID，得到项目ID列表[项目ID列表],方法ID：SE20210724145741533389732A93233", notes = "/projectAndProductController/V1/selectProjectListByProductId?paraId=paraId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paraId", value = "父ID", dataType = "String", required = true)
    })
    @PostMapping("/V1/selectProjectListByProductId")
    public List<String> selectProjectListByProductId(@RequestParam(value = "paraId") String paraId) {
        return projectAndProductService.selectProjectListByProductId(paraId);
    }
}